
import api from "./api";
// 登录表单提交事件
document.getElementById('login-form')?.addEventListener('submit', async function (e) {
    e.preventDefault();
 alert("登录");
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;


    try {
        const response = await api.auth.login({ username, password });
        if (response.success) {
            tokenManager.setUser(response.data.id, response.data.username);
            alert(tokenManager.getUserid() + " " + tokenManager.getUsername());
            showMessage('success', '登录成功');
            window.location.href = 'index.html';
        } else {
            showMessage('error', response.message);
        }
    } catch (error) {
        showMessage('error', '登录失败');
    }
});

// 注册表单提交事件
document.getElementById('register-form')?.addEventListener('submit', async function (e) {
    e.preventDefault();
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const email = document.getElementById('email').value;
    const phone = document.getElementById('phone').value;

    try {
        const response = await auth.register({ username, password, email, phone });
        if (response.success) {
            showMessage('success', '注册成功，请登录');
            window.location.href = 'login.html';
        } else {
            showMessage('error', response.message);
        }
    } catch (error) {
        showMessage('error', '注册失败');
    }
});